<template>
  <el-dialog :title="!dataForm.id ? '新增汇总信息' : '修改汇总信息'" :close-on-click-modal="false" :visible.sync="visible"
    width="800px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="140px">
      <el-form-item label="汇总名称" prop="hzName">
        <el-input v-model="dataForm.hzName" placeholder="请输入汇总名称" style="width: 90%"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
  /*!
   * 绩效考核-测评统计编辑
   *
   * @author zmk
   * @date 2021-07-08 09:30:00
   */
  import {
    getObj,
    addObj,
    putObj
  } from '@/api/ceping/cepinghzglbhz'

  export default {
    data() {
      return {
        visible: false,
        canSubmit: false,
        disabled: false,
        dataForm: {
          id: 0,
          hzName: '',
        },
        dataRule: {
          hzName: [{
              required: true,
              message: '汇总名称不能为空',
              trigger: 'blur'
            },
            {
              max: 100,
              message: '长度在最长100个字符',
              trigger: 'blur'
            }
          ],
        }
      }
    },
    methods: {
      //初始化
      csh() {

        this.dataForm = {
          id: 0,
          hzName: '',
        };
      },
      init(id) {
        this.csh();
        this.dataForm.id = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.disabled = true;
            getObj(this.dataForm.id).then(response => {
              this.dataForm = response.data.data
            })
          } else {
            this.disabled = false;
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
              putObj(this.dataForm).then(data => {
                this.$notify.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              }).catch(() => {
                this.canSubmit = true;
              });
            } else {
              addObj(this.dataForm).then(data => {
                this.$notify.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              }).catch(() => {
                this.canSubmit = true;
              });
            }
          }
        })
      }
    }
  }

</script>
